﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using TinyLib.Input;

namespace GodBuilderIdea.Tools
{
    public class BucketTool : ITool
    {
        private TileBoard m_tiles;
        private EnvTile.EnvironmentType m_typeToChange;

        int stackcheck = 0;

        public BucketTool(TileBoard tiles, EnvTile.EnvironmentType type)
        {
            m_tiles = tiles;
            m_tileType = type;
        }

        public override void MouseLeftClick(int x, int y)
        {
            if (m_activePlayer.m_actions < 5)
            {
                return;
            }
            //Update tiles on the board.
            bool withdrawn_actions = false;
            for(int i = 0; i < m_tiles.Tiles.Count; i++)
            {
                if (m_tiles.Tiles[i].Rectangle.Contains(new Point(x, y)))
                {
                    stackcheck = 0;
                    //m_tiles[i].m_changed_type = true;
                    //m_tiles[i].m_type = m_activePlayer.m_current_env_type; //m_tileType
                    m_typeToChange = m_tiles.Tiles[i].m_type;
                    if (m_tiles.Tiles[i].m_type != EnvTile.EnvironmentType.DEFAULT
                        && m_tiles.Tiles[i].m_type != EnvTile.EnvironmentType.LAVA
                        && m_activePlayer.m_currentTool.m_tileType != m_typeToChange)
                    {
                        if (!withdrawn_actions)
                        { m_activePlayer.m_actions -= 5; withdrawn_actions = true; }
                        CheckAdjecentTiles(i); }
                    break;
                }
            }
        }

        public void CheckAdjecentTiles(int index)
        {
            stackcheck++;
            if (stackcheck > 2000)
            {
              //  stackcheck = 0;
                return;
            }
            //Change adjecent tiels to selected type.
            if (m_tiles.Tiles[index].m_type == m_typeToChange)
            {
                m_tiles.Tiles[index].m_type = m_tileType;
                m_tiles.Tiles[index].m_changed_type = true;

                //Check left of index
                if (index > m_tiles.Height - 1)
                {
                    CheckAdjecentTiles(index - m_tiles.Height);
                }

                //Check over index
                if (index % m_tiles.Height != 0)
                {
                    CheckAdjecentTiles(index - 1);
                }

                //Check under index
                if (index % m_tiles.Height != m_tiles.Height - 1)
                {
                    CheckAdjecentTiles(index + 1);
                }
                //Check right of index
                if (index / m_tiles.Height < m_tiles.Width - 1)
                {
                    CheckAdjecentTiles(index + m_tiles.Height);
                }
            }
        }

        public override void MouseRightClick(int x, int y)
        {
            //Do nothing.
        }
    }
}
